<template>
  <div>
    <!-- 床位管理 -->

    <!-- 表单 -->
    <el-form size="small" :model="form" :inline="true" label-width="70px">
      <el-form-item label="校 区" style="font-weight:bold;" v-show="(clientRole != 1 && clientRole != 2)">
        <el-select v-model="form.campusId" placeholder="请选择校区" style="width:150px;" :disabled="clientRole == 6" clearable>
          <el-option v-for="item in CampusName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼 宇" style="font-weight:bold;" v-show="(clientRole != 1 && clientRole != 2)">
        <el-select v-model="form.buildName" placeholder="请选择楼宇" style="width:150px;" clearable>
          <el-option v-for="item in buildName" :key="item.id" :label="item.name" :value="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房间号" style="font-weight:bold;" v-show="(clientRole != 1 && clientRole != 2)">
        <el-input v-model="form.roomNum" placeholder="(如304)" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="学生姓名" style="font-weight:bold;">
        <el-input v-model="form.userName" placeholder="请输入学生姓名" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="学 号" style="font-weight:bold;">
        <el-input v-model="form.userNum" placeholder="请输入学号" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="学生院系" style="font-weight:bold;" v-show="searchType == 1">
        <el-select v-model="form.userCollegeId" placeholder="请选择" style="width:150px;"
          :disabled="(clientRole == 1 || clientRole == 2) && searchType == 1" clearable>
          <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="床位院系" style="font-weight:bold;" v-show="searchType == 2">
        <el-select v-model="form.bedCollegeId" placeholder="请选择" style="width:150px;"
          :disabled="(clientRole == 1 || clientRole == 2) && searchType == 2" clearable>
          <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="辅导员" style="font-weight:bold;">
        <el-select v-model="form.adminName" placeholder="请选择" style="width:150px;" clearable>
          <el-option v-for="item in teachers" :key="item.id" :label="item.name" :value="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年 级" style="font-weight:bold;" v-if="grades.length == 0">
        <el-input v-model="form.grade" placeholder="(如2021级)" style="width:150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="年 级" style="font-weight:bold;" v-if="grades.length > 0">
        <el-select v-model="form.grade" placeholder="" style="width:150px;">
          <el-option v-for="item in grades" :key="item.id" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生类型" style="font-weight:bold;" v-if="stuTypeF.length == 0">
        <el-select v-model="form.type" placeholder="请选择学生类型" style="width:150px;" clearable>
          <el-option v-for="item in stuType" :key="item.id" :label="item.type" :value="item.value"></el-option>
          <!-- <el-option label="本科生" value="0"></el-option>
          <el-option label="硕士生" value="1"></el-option>
          <el-option label="博士生" value="2"></el-option>
          <el-option label="留学生" value="3"></el-option>
          <el-option label="交换生" value="4"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="学生类型" style="font-weight:bold;" v-if="stuTypeF.length > 0">
        <el-select v-model="form.type" placeholder="" style="width:150px;">
          <el-option v-for="item in stuTypeF" :key="item.id" :label="item.type" :value="item.value"></el-option>
          <!-- <el-option label="本科生" value="0"></el-option>
          <el-option label="硕士生" value="1"></el-option>
          <el-option label="博士生" value="2"></el-option>
          <el-option label="留学生" value="3"></el-option>
          <el-option label="交换生" value="4"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="性 别" style="font-weight:bold;">
        <el-select v-model="form.sex" placeholder="请选择性别" style="width:150px;" clearable>
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="住宿状态" style="font-weight:bold;">
        <el-select v-model="form.state" placeholder="请选择住宿状态" style="width:150px;" clearable>
          <el-option label="使用中" value="1"></el-option>
          <el-option label="空床" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="院系查询方式" label-width="100px">
        <el-radio-group v-model="searchType">
          <el-radio label="1">学生院系</el-radio>
          <el-radio label="2">床位院系</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!-- 表单 -->
    <div class="buttonList">
      <el-button class="exportBtn" size="small" type="success" plain icon="el-icon-download" @click="exportBedExcel"
        v-show="clientRole != 5">导出床位信息</el-button>
      <div>
        <el-button size="small" class="search_button" type="primary" icon="el-icon-search"
          @click="getDataList">查询</el-button>
        <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>

      </div>
    </div>

    <el-divider></el-divider>

    <!-- 表格 -->
    <el-table size="medium" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
      @selection-change="handleSelectionChange" border v-loading="tableDataLoading">
      <!-- <el-table-column prop="id" label="id" align="center"></el-table-column> -->
      <el-table-column prop="campusName" label="校区" align="center" min-width="100"></el-table-column>
      <el-table-column prop="userCollegeName" label="学生院系" align="center"></el-table-column>
      <el-table-column prop="bedCollegeName" label="床位院系" align="center"></el-table-column>
      <el-table-column prop="userName" label="姓名" align="center"></el-table-column>
      <el-table-column prop="userNum" label="学号" align="center"></el-table-column>
      <el-table-column prop="sex" label="性别" align="center">
        <template slot-scope="scope">
          <span v-if="(scope.row.sex == 0)">女</span>
          <span v-if="(scope.row.sex == 1)">男</span>
        </template>
      </el-table-column>
      <el-table-column prop="grade" label="年级" align="center"></el-table-column>
      <el-table-column prop="type" label="学生类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">本科生</span>
          <span v-if="scope.row.type == 1">硕士生</span>
          <span v-if="scope.row.type == 2">博士生</span>
          <span v-if="scope.row.type == 3">留学生</span>
          <span v-if="scope.row.type == 4">交换生</span>
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" align="center"></el-table-column>
      <el-table-column prop="adminName" label="辅导员" align="center"></el-table-column>

      <el-table-column prop="buildName" label="楼宇" align="center"></el-table-column>
      <el-table-column prop="roomNum" label="房间号" align="center"></el-table-column>
      <el-table-column prop="bedCode" label="床位号" align="center"></el-table-column>
      <el-table-column prop="dormType" label="房间类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.dormType == 0">学生宿舍</span>
          <span v-if="scope.row.dormType == 1">博士宿舍</span>
          <span v-if="scope.row.dormType == 2">公用房</span>
          <span v-if="scope.row.dormType == 3">爱心房</span>
          <span v-if="scope.row.dormType == 4">工具房</span>
        </template>
      </el-table-column>
      <el-table-column prop="sexType" label="男女生宿舍" align="center" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.sexType == 0">女生宿舍</span>
          <span v-if="scope.row.sexType == 1">男生宿舍</span>
        </template>
      </el-table-column>
      <el-table-column prop="peoNum" label="可住人数" align="center" width="80"></el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="(scope.row.state == 0)" style="color:red;">空床</span>
          <span v-if="(scope.row.state == 1)" style="color:#67C23A;">使用中</span>
        </template>
      </el-table-column>
      <el-table-column prop="inTime" label="入住时间" align="center" width="100"></el-table-column>
      <el-table-column prop="outTime" label="退宿时间" align="center" width="100"></el-table-column>
      <el-table-column prop="state" label="操作" align="center" fixed="right" width="180"
        v-if="clientRole == 115 || clientRole == 6">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-refresh" @click="changeState(scope.row)">调整床位</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="changeCollege(scope.row)">修改院系</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 修改院系弹窗 -->
    <el-dialog title="修改院系信息" :visible.sync="dialog_changeCollege">
      <el-form label-width="80px">
        <el-form-item label="院 系" style="font-weight:bold;">
          <el-select v-model="formChangeCollege.collegeId" placeholder="请选择院系" clearable>
            <el-option v-for=" item  in  CollegeName " :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_changeCollege = false">取 消</el-button>
        <el-button type="primary" @click="updateCollege" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改院系弹窗 -->

    <!-- 修改床位弹窗 -->
    <el-dialog title="调整床位" :visible.sync="dialog_change">
      <el-form label-width="80px" style="margin-left:20%;">
        <el-form-item label="姓名 :">
          <el-input :disabled="showInfor" v-model.trim="formChange.userName" style="width:60%" placeholder="请输入姓名"
            clearable></el-input>
        </el-form-item>
        <el-form-item label="学号 :">
          <el-input :disabled="showInfor" v-model.trim="formChange.userNum" style="width:60%" placeholder="请输入学号"
            clearable></el-input>
        </el-form-item>
        <el-form-item label="床位操作 :">
          <el-radio-group v-model="formChange.state">
            <el-radio label="0">退宿</el-radio>
            <el-radio label="1">入住</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" @click="updateBedDetail" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改床位弹窗 -->


    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>

  </div>
</template>

<script>
import { getList, exportBedExcel, updateBedDetail, getInformation, updateCollege } from "_api/DormManage/Bed/index.js"
import { getCampusName, getBuildingName } from '_api/DormManage/Building/index.js'
import { getCollegeName, getTeacher } from '_api/DormManage/College/index.js'
export default {
  data() {
    return {
      searchType: '1',
      clientRole: "",
      form: {
        adminName: "",
        buildName: "",
        campusId: "",
        userCollegeId: "",
        bedCollegeId: "",
        type: "",
        grade: "",
        current: 0,
        field: "",
        merchantId: 0,
        order: "",
        pageSize: 10,
        roomNum: "",
        state: "",
        sex: ""
      },
      tableData: [],
      total: 0,
      CampusName: [],
      buildName: [],
      CollegeName: [],
      grades: [],
      stuType: [
        { id: 0, type: '本科生', value: 0 },
        { id: 1, type: '硕士生', value: 1 },
        { id: 2, type: '博士生', value: 2 },
        { id: 3, type: '留学生', value: 3 },
        { id: 4, type: '交换生', value: 4 },
      ],
      stuTypeF: [],
      timeNow: "",
      tableDataLoading: true,
      dialog_change: false,
      dialog_changeCollege: false,
      showInfor: false,
      formChange: {
        userName: "",
        userNum: "",
        state: "",
        bedId: ""
      },
      formChangeCollege: {
        collegeId: "",
        bedId: ""
      },
      nowState: "",
      teachers: []
    }
  },
  watch: {
    form: {
      // 执行方法
      handler(oldValue, newValue) {
        getBuildingName({ campusId: newValue.campusId, type: 0 }).then(res => {
          if (res.status == 200) {
            this.buildName = res.data.data
          }
        })
        if (newValue.userCollegeId) {
          getTeacher({ collegeId: newValue.userCollegeId }).then(res => {
            if (res.status == 200) {
              this.teachers = res.data.data
            }
          })
        }
        if (newValue.bedCollegeId) {
          getTeacher({ collegeId: newValue.bedCollegeId }).then(res => {
            if (res.status == 200) {
              this.teachers = res.data.data
            }
          })
        }
      },
      deep: true, // 深度监听
      // immediate: true  // 第一次改变就执行
    },
    searchType: {
      // 执行方法
      handler(oldValue, newValue) {
        this.getCampusName()
      },
    },

  },
  methods: {
    handleSelectionChange() {

    },
    changeCollege(e) {
      this.formChangeCollege = {
        collegeId: "",
        bedId: ""
      }
      this.formChangeCollege.bedId = e.id
      this.CollegeName.forEach(item => {
        if (item.name == e.collegeName) {
          this.formChangeCollege.collegeId = item.id
        }
      })
      this.dialog_changeCollege = true
    },
    updateCollege() {
      updateCollege(this.formChangeCollege).then(res => {
        if (res.data.success == true) {
          this.$message.success('操作成功!')
          this.dialog_changeCollege = false
          this.getList()
        } else {
          this.$message(res.data.message)
        }
      })
    },
    changeState(e) {
      this.formChange = {
        userName: "",
        userNum: "",
        state: "",
        bedId: ""
      }
      this.formChange.bedId = e.id
      this.formChange.state = e.state
      this.nowState = e.state
      if (e.userName) {
        this.formChange.userName = e.userName
      }
      if (e.userNum) {
        this.formChange.userNum = e.userNum
      }
      if (e.userName && e.userNum) {
        this.showInfor = true
      } else {
        this.showInfor = false
      }
      this.dialog_change = true
    },
    updateBedDetail() {
      if (this.formChange.state == this.nowState && this.nowState == 1) {
        this.$message('该床位状态已经为使用中')
        return false
      }
      if (this.formChange.state == this.nowState && this.nowState == 0) {
        this.$message('该床位状态已经为空床')
        return false
      }
      // if (this.nowState == 0 && this.formChange.state == 1) {
      //   if (this.formChange.userName == "" || this.formChange.userNum == "") {
      //     this.$message('请将信息补充完整')
      //     return false
      //   }
      // }
      updateBedDetail(this.formChange).then(res => {
        if (res.data.success == true) {
          this.$message.success('操作成功!')
          this.dialog_change = false
          this.getList()
        } else {
          this.$message(res.data.message)
        }
      })
    },
    //重置
    reset() {
      if (this.clientRole == 1 || this.clientRole == 2) {
        this.form.current = 0
        this.form.pageSize = 10
        this.form.merchantId = 0
        this.form.userName = ""
        this.form.userNum = ""
        this.form.roomNum = ""
        this.form.grade = ""
        this.form.type = ""
      } if (this.clientRole == 6) {
        this.form.current = 0
        this.form.pageSize = 10
        this.form.merchantId = 0
        this.form.userCollegeId = ""
        this.form.bedCollegeId = ""
        this.form.buildName = ""
        this.form.roomNum = ""
        this.form.userName = ""
        this.form.userNum = ""
        this.form.grade = ""
        this.form.type = ""
        this.form.state = ""
      } else {
        this.form = {
          buildName: "",
          campusId: "",
          userCollegeId: "",
          bedCollegeId: "",
          type: "",
          grade: "",
          current: 0,
          field: "",
          merchantId: 0,
          order: "",
          pageSize: 10,
          roomNum: "",
          state: ""
        }
      }
      this.getCampusName()
    },
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
      // console.log(_this.gettime)
      this.timeNow = _this.gettime
    },
    exportBedExcel() {
      exportBedExcel(this.form).then(res => {
        // console.log(res.data,"121212121")
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '床位信息报表_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    // prev(e) {
    //   //上一页
    //   this.form.current = e-1
    //   this.getList()
    // },
    // next(e) {
    //   //下一页
    //   this.form.current = e-1
    //   this.getList()
    // },
    handleCurrentChange(e) {
      //当前页
      this.form.current = (e - 1)
      this.getList()
    },
    // 查询按钮
    getDataList() {
      this.form.current = 0
      this.getList()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getList()
    },
    //列表查询
    getList() {
      if (this.searchType == 1) {
        this.form.bedCollegeId = ''
      }
      if (this.searchType == 2) {
        this.form.userCollegeId = ''
      }
      getList(
        this.form
      ).then(res => {
        // console.log(res)
        if (res.data.success == true) {
          var list = res.data.data.list
          this.tableData = list
          this.total = res.data.data.total
          this.tableDataLoading = false
        } else {
          this.tableData = res.data.data
          this.total = 0
          this.tableDataLoading = false
          this.$message.info(res.data.message)
        }
      })
    },
    //获取校区名称
    getCampusName() {
      getCampusName().then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.CampusName = res.data.data
          if (window.localStorage.getItem('clientRole') == 6) {
            this.CampusName.forEach(item => {
              if (item.name == window.localStorage.getItem('campusName')) {
                this.form.campusId = item.id
              }
            })
          }
          this.getBuildingName()

        }
      })
    },
    //获取楼宇名称
    getBuildingName() {
      getBuildingName({ campusId: this.form.campusId, type: 0 }).then(res => {
        // console.log(res)
        if (res.status == 200) {
          this.buildName = res.data.data
        }
        this.getCollegeName()
      })
    },
    // 获取学院名称
    getCollegeName() {
      getCollegeName().then(res => {
        if (res.status == 200) {
          this.CollegeName = res.data.data
          if (this.clientRole == 2) {
            this.CollegeName.forEach(item => {
              if (item.name == window.localStorage.getItem('collegeName')) {
                this.form.userCollegeId = item.id
                this.form.bedCollegeId = item.id
              }
            })
            // getTeacher({ collegeId: this.form.userCollegeId }).then(res => {
            //   if (res.status == 200) {
            //     this.teachers = res.data.data
            //   }
            // })
          }
        }
        this.getInformation()
      })
    },
    getInformation() {
      getInformation({ adminNum: window.sessionStorage.account }).then(res => {
        this.CollegeName = res.data.data.colleges
        if (this.clientRole == 1) {
          if (this.CollegeName.length == 1) {
            this.form.userCollegeId = this.CollegeName[0].id
            this.form.bedCollegeId = this.CollegeName[0].id
          }

          if (res.data.data.gradeName) {
            this.grades = res.data.data.gradeName
            this.form.grade = this.grades[0]
          }
          if (res.data.data.stuType) {
            res.data.data.stuType.forEach(itemOut => {
              this.stuType.forEach(itemIn => {
                if (itemOut == itemIn.value) {
                  if (!this.stuTypeF.includes(itemIn)) {
                    this.stuTypeF.push(itemIn)
                  }
                }
              })
            })
            this.form.type = this.stuTypeF[0].value
          }
        }

        this.getList()
      })
    },
  },
  created() {
    // this.getList()
    this.clientRole = window.localStorage.getItem('clientRole')
    this.getCampusName()
    // this.getBuildingName()
    // this.getCollegeName()
  },
};
</script>
<style lang="scss" scoped>
.buttonList {
  display: flex;
  width: 70%;
  justify-content: space-between;
}

.exportBtn {
  margin-left: 70px;
}

.search_button {
  // margin-left: 80px;
}

.reset_button {
  margin-left: 10px;
}

.newly_button {
  margin-left: 5px;
}

.pagination {
  float: right;
  margin-top: 10px;
}
</style>
